// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#include <melon/benchmark.h>
#include <melon/init/init.h>

#include <pollux/benchmarks/ExpressionBenchmarkBuilder.h>
#include <pollux/functions/registerer.h>
#include <pollux/functions/prestosql/arithmetic.h>
#include <pollux/functions/prestosql/string_functions.h>
#include <pollux/functions/prestosql/url_functions.h>
#include <pollux/functions/remote/client/remote.h>
#include <pollux/functions/remote/utils/remote_function_service_provider.h>
#include <pollux/type/type.h>

using namespace kumo::pollux;
using namespace kumo::pollux::functions;

TURBO_FLAG(int32_t,batch_size, 1000, "Batch size for benchmarks");

int main(int argc, char** argv) {
  melon::Init init(&argc, &argv);
  memory::MemoryManager::initialize({});

  auto param = startLocalThriftServiceAndGetParams();
  RemoteVectorFunctionMetadata metadata;
  metadata.location = param.serverAddress;

  // Register the remote adapter for PlusFunction
  auto plusSignatures = {exec::FunctionSignatureBuilder()
                             .returnType("bigint")
                             .argumentType("bigint")
                             .argumentType("bigint")
                             .build()};
  registerRemoteFunction("remote_plus", plusSignatures, metadata);
  // Registers the actual function under a different prefix. This is only
  // needed when thrift service runs in the same process.
  register_function<PlusFunction, int64_t, int64_t, int64_t>(
      {param.functionPrefix + ".remote_plus"});
  // register this function again, because the benchmark builder somehow doesn't
  // recognize the function registered above (remote.xxx).
  register_function<PlusFunction, int64_t, int64_t, int64_t>({"plus"});

  // Register the remote adapter for SubstrFunction
  auto substrSignatures = {exec::FunctionSignatureBuilder()
                               .returnType("varchar")
                               .argumentType("varchar")
                               .argumentType("integer")
                               .build()};
  registerRemoteFunction("remote_substr", substrSignatures, metadata);
  register_function<SubstrFunction, Varchar, Varchar, int32_t>(
      {param.functionPrefix + ".remote_substr"});
  register_function<SubstrFunction, Varchar, Varchar, int32_t>({"substr"});

  // Register the remote adapter for UrlEncodeFunction
  auto urlSignatures = {exec::FunctionSignatureBuilder()
                            .returnType("varchar")
                            .argumentType("varchar")
                            .build()};
  registerRemoteFunction("remote_url_encode", urlSignatures, metadata);
  register_function<UrlEncodeFunction, Varchar, Varchar>(
      {param.functionPrefix + ".remote_url_encode"});
  register_function<UrlEncodeFunction, Varchar, Varchar>({"url_encode"});

  ExpressionBenchmarkBuilder benchmarkBuilder;

  VectorFuzzer::Options opts;
  opts.vectorSize = FLAGS_batch_size;
  opts.stringVariableLength = true;
  opts.containerVariableLength = true;
  VectorFuzzer fuzzer(opts, benchmarkBuilder.pool());
  auto vectorMaker = benchmarkBuilder.vectorMaker();

  // benchmark comparaing PlusFunction running locally (same thread)
  // and running with RemoteFunction (different threads).
  benchmarkBuilder
      .addBenchmarkSet(
          "local_vs_remote_plus",
          vectorMaker.row_vector(
              {"c0", "c1"},
              {fuzzer.fuzzFlat(BIGINT()), fuzzer.fuzzFlat(BIGINT())}))
      .addExpression("local_plus", "plus(c0, c1) ")
      .addExpression("remote_plus", "remote_plus(c0, c1) ")
      .withIterations(1000);

  // benchmark comparaing SubstrFunction running locally (same thread)
  // and running with RemoteFunction (different threads).
  benchmarkBuilder
      .addBenchmarkSet(
          "local_vs_remote_substr",
          vectorMaker.row_vector(
              {"c0", "c1"},
              {fuzzer.fuzzFlat(VARCHAR()), fuzzer.fuzzFlat(INTEGER())}))
      .addExpression("local_substr", "substr(c0, c1) ")
      .addExpression("remote_substr", "remote_substr(c0, c1) ")
      .withIterations(1000);

  // benchmark comparaing UrlEncodeFunction running locally (same thread)
  // and running with RemoteFunction (different threads).
  benchmarkBuilder
      .addBenchmarkSet(
          "local_vs_remote_url_encode",
          vectorMaker.row_vector({"c0"}, {fuzzer.fuzzFlat(VARCHAR())}))
      .addExpression("local_url_encode", "url_encode(c0) ")
      .addExpression("remote_url_encode", "remote_url_encode(c0) ")
      .withIterations(1000);

  benchmarkBuilder.registerBenchmarks();
  benchmarkBuilder.testBenchmarks();
  melon::runBenchmarks();
  return 0;
}
